﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using trangbaocao.Models;
using System.Data;
using System.Data.OracleClient;
using Microsoft.Reporting.WebForms;

namespace trangbaocao.Reports.Move_Device
{
    public partial class ReportMoveDevice : System.Web.UI.Page
    {
        StationsModel StationsModel = new StationsModel();
        Main_StationsModel Main_StationsModel = new Main_StationsModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session != null && Convert.ToInt32(Session["quyen_id"]) > 2)
                {
                    Radio1.Visible = false;
                }
                List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
                List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Convert.ToInt32(main_StationsModel[0].id));
                cbtrungtam.DataSource = main_StationsModel;
                cbtrungtam.DataTextField = "name";
                cbtrungtam.DataValueField = "id";
                cbtrungtam.DataBind();

                cbtram.DataSource = stationModel;
                cbtram.DataTextField = "name";
                cbtram.DataValueField = "id";
                cbtram.DataBind();

                //cbnam.DataSource = getYear();
                //cbnam.DataTextField = "nam";
                //cbnam.DataValueField = "nam";
                //cbnam.DataBind();

                //for (int i = 1; i <= 12; i++)
                //{
                //    cbthang.Items.Add(i.ToString());
                //    cbthang2.Items.Add(i.ToString());
                //}
                ////cbthang.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
                //cbthang2.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
            }
        }

        protected void Radio1_CheckedChanged(object sender, EventArgs e)
        {
            if (Radio1.Checked)
            {
                cbtrungtam.Enabled = false;
                cbtram.Enabled = false;
            }
            if (Radio2.Checked)
            {
                cbtrungtam.Enabled = true;
                cbtram.Enabled = true;
            }
        }

        protected void radio_tram_group_CheckedChanged(object sender, EventArgs e)
        {
            if (radio_tram_group.Checked)
            {
                cbtram.Enabled = false;
            }
            if (radio_tram.Checked)
            {
                cbtram.Enabled = true;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ReportViewer1.Reset();
            Main_Sort_DeviceModel Main_Sort_DeviceModel = new Main_Sort_DeviceModel();
            List<Main_Sort_DeviceModel> main_Sort_DeviceModel = Main_Sort_DeviceModel.getChungloaithietbi();

            ReportViewer1.LocalReport.ReportPath = "Reports/Move_Device/Report_Move_Device.rdlc";
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("dts_device_move_ds", getDslamConfiguration().Tables[0]));

            ReportViewer1.DataBind();
            ReportViewer1.LocalReport.Refresh();
        }

        protected void cbtrungtam_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Convert.ToInt32(cbtrungtam.SelectedValue));
            cbtram.DataSource = stationModel;
            cbtram.DataTextField = "name";
            cbtram.DataValueField = "id";
            cbtram.DataBind();
        }
        public DataSet getDslamConfiguration()
        {
            string sql = "";
            //string nam = cbnam.SelectedValue;
            //string thang = cbthang.SelectedValue;
            //string thang2 = cbthang2.SelectedValue;
            string trung_tam_id = cbtrungtam.SelectedValue;
            string tram_id = cbtram.SelectedValue;

            string sql_tram = "";
            if (radio_tram.Checked)
                sql_tram = " and (a.stations_id_from=" + tram_id + " or a.stations_id_to=" + tram_id + ")";
            if (Radio2.Checked)
            {
                sql = "SELECT a.ten_tb_new ten_tb_new, a.ma_ht_new ma_he_thong_new, d.main_stations_name ten_tt_new, d.stations_name_to ten_tram_new, a.ten_tb_old ten_tb_old, a.ma_ht_old ma_he_thong_old, b.main_stations_name ten_tt_old, b.stations_name_from ten_tram_old, ";
                sql +="to_char(a.date_move,'dd/mm/yyyy') ngay_dieu_chuyen, a.nv_dichchuyen nv_dieu_chuyen, a.note ghi_chu FROM MOVE_DEVICE a, ";
                sql +="(SELECT a.id, a.stations_id_from stations_id_from, b.name stations_name_from, c.name main_stations_name from move_device a, stations b, main_stations c where a.stations_id_from = b.id and b.main_stations_id=c.id ) b, ";
                sql +="(SELECT a.id, a.stations_id_to stations_id_to, b.name stations_name_to, c.name main_stations_name from ";
                sql +=" move_device a, stations b, main_stations c where a.stations_id_to = b.id and b.main_stations_id=c.id ) d";
                sql += " where a.id = b.id and a.id=d.id and (a.stations_id_from in (select t.id from stations t where t.main_stations_id=" + trung_tam_id + ") or a.stations_id_to in (select t.id from stations t where t.main_stations_id=" + trung_tam_id + ")) " + sql_tram + "order by a.id";
            }
            if (Radio1.Checked)
            {
                sql = "SELECT a.ten_tb_new ten_tb_new, a.ma_ht_new ma_he_thong_new, d.main_stations_name ten_tt_new, d.stations_name_to ten_tram_new, a.ten_tb_old ten_tb_old, a.ma_ht_old ma_he_thong_old, b.main_stations_name ten_tt_old, b.stations_name_from ten_tram_old,  to_char(a.date_move,'dd/mm/yyyy') ngay_dieu_chuyen, a.nv_dichchuyen nv_dieu_chuyen, a.note ghi_chu FROM MOVE_DEVICE a, ";
                sql += " (SELECT a.id, a.stations_id_from stations_id_from, b.name stations_name_from, c.name main_stations_name  from move_device a, stations b, main_stations c where a.stations_id_from = b.id and b.main_stations_id=c.id) b, ";
                sql += " (SELECT a.id, a.stations_id_to stations_id_to, b.name stations_name_to, c.name main_stations_name  from move_device a, stations b, main_stations c where a.stations_id_to = b.id and b.main_stations_id=c.id) d ";
                sql += " where a.id = b.id and a.id=d.id order by a.id";
            }

            OracleConnection conn = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn);
            OracleDataAdapter oda = new OracleDataAdapter(omd);
           
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            oda.Fill(ds, "0");
            conn.Close();
            return ds;

        }
        public DataSet getYear()
        {
            string sql = "SELECT distinct nam from fuel order by nam desc";
            OracleConnection conn = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn);
            OracleDataAdapter oda = new OracleDataAdapter(omd);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            oda.Fill(ds, "0");
            conn.Close();
            return ds;
        }
    }
}